package com.sds.sweng.swat.miplatform.service.validation;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Node;

public class ValidationInfo {

	public static final String validationSeparator = ";";
	public static final String infoSeparator = ",";
	public static final String valueSeparator = ":";

	public Node component;

	public String componentName;
	public String id;
	public String businessName = StringUtils.EMPTY;
	public String type = ValidationType.NONE;
	public String minLength = StringUtils.EMPTY;
	public String maxLength = StringUtils.EMPTY;
	public String min = StringUtils.EMPTY;
	public String max = StringUtils.EMPTY;
	public String compOp = StringUtils.EMPTY;
	public String compTarget = StringUtils.EMPTY;
	public boolean required;
	public String requiredGroup = StringUtils.EMPTY;

	public boolean partied;

	public ValidationInfo(Node component, String id) {
		this.component = component;
		this.componentName = component.getName();
		this.id = id;
	}

	public void setInfoFromString(String validationString) {
		if (validationString == null) {
			return;
		}

		validationString = StringUtils.remove(validationString, "\n");
		validationString = StringUtils.remove(validationString, "\r");

		required = false;
		partied = false;

		String[] infos = StringUtils.split(validationString, infoSeparator);

		for (int i=0;i<infos.length;i++) {
			String info = StringUtils.trim(infos[i]);

			if ("required".equalsIgnoreCase(info)) {
				required = true;
			} else {
				String[] keyValue = StringUtils.split(info, valueSeparator, 2);

				if (keyValue.length == 2) {
					String key = keyValue[0];
					String value = StringUtils.trim(keyValue[1]);

					if ("type".equalsIgnoreCase(key)) {
						if (!ValidationType.isValidType(value)) {
							type = ValidationType.NONE;
							partied = false;
						} else {
							type = value;
							partied = true;
						}
					} else if ("name".equalsIgnoreCase(key)) {
						businessName = toValidString(value);
					} else if ("reqgrp".equalsIgnoreCase(key)) {
						requiredGroup = toValidString(value);
					} else if ("minl".equalsIgnoreCase(key)) {
						minLength = toPositiveInteger(value);
					} else if ("maxl".equalsIgnoreCase(key)) {
						maxLength = toPositiveInteger(value);
					} else if ("min".equalsIgnoreCase(key)) {
						min = toNumber(value);
					} else if ("max".equalsIgnoreCase(key)) {
						max = toNumber(value);
					} else if ("cop".equalsIgnoreCase(key)) {
						if (!ValidationCompareOperator.isValidOperator(value)) {
							compOp = ValidationCompareOperator.NONE;
						} else {
							compOp = value;
						}
					} else if ("ctgt".equalsIgnoreCase(key)) {
						compTarget = value;
					}
				} else if (keyValue.length == 1) {
					String key = keyValue[0];

					if ("type".equalsIgnoreCase(key)) {
						type = ValidationType.NONE;
						partied = false;
					} else if ("name".equalsIgnoreCase(key)) {
						businessName = StringUtils.EMPTY;
					} else if ("reqgrp".equalsIgnoreCase(key)) {
						requiredGroup = StringUtils.EMPTY;
					} else if ("minl".equalsIgnoreCase(key)) {
						minLength = StringUtils.EMPTY;
					} else if ("maxl".equalsIgnoreCase(key)) {
						maxLength = StringUtils.EMPTY;
					} else if ("min".equalsIgnoreCase(key)) {
						min = StringUtils.EMPTY;
					} else if ("max".equalsIgnoreCase(key)) {
						max = StringUtils.EMPTY;
					} else if ("cop".equalsIgnoreCase(key)) {
						compOp = StringUtils.EMPTY;
					} else if ("ctgt".equalsIgnoreCase(key)) {
						compTarget = StringUtils.EMPTY;
					}
				}
			}

		}
	}

	public String toValidString(String str) {
		str = StringUtils.trim(str);

		if ("null".equalsIgnoreCase(str) || StringUtils.isEmpty(str)) {
			return StringUtils.EMPTY;
		}

		return str;
	}

	public String toString() {
		StringBuilder validationString = new StringBuilder("id");
		validationString.append(valueSeparator);
		validationString.append(id);
		validationString.append(infoSeparator);
		validationString.append("name");
		validationString.append(valueSeparator);
		validationString.append(businessName);
		validationString.append(infoSeparator);
		validationString.append("type");
		validationString.append(valueSeparator);
		validationString.append(type);
		if (required) {
			validationString.append(infoSeparator);
			validationString.append("required");
		}
		if (!StringUtils.isEmpty(requiredGroup)) {
			validationString.append(infoSeparator);
			validationString.append("reqgrp");
			validationString.append(valueSeparator);
			validationString.append(requiredGroup);
		}
		if (!StringUtils.isEmpty(minLength)) {
			validationString.append(infoSeparator);
			validationString.append("minl");
			validationString.append(valueSeparator);
			validationString.append(minLength);
		}
		if (!StringUtils.isEmpty(maxLength)) {
			validationString.append(infoSeparator);
			validationString.append("maxl");
			validationString.append(valueSeparator);
			validationString.append(maxLength);
		}
		if (!StringUtils.isEmpty(min)) {
			validationString.append(infoSeparator);
			validationString.append("min");
			validationString.append(valueSeparator);
			validationString.append(min);
		}
		if (!StringUtils.isEmpty(max)) {
			validationString.append(infoSeparator);
			validationString.append("max");
			validationString.append(valueSeparator);
			validationString.append(max);
		}
		if (!StringUtils.isEmpty(compOp)) {
			validationString.append(infoSeparator);
			validationString.append("cop");
			validationString.append(valueSeparator);
			validationString.append(compOp);
		}
		if (!StringUtils.isEmpty(compTarget)) {
			validationString.append(infoSeparator);
			validationString.append("ctgt");
			validationString.append(valueSeparator);
			validationString.append(compTarget);
		}

		return validationString.toString();
	}

	public static String toNumber(Object number) {
		return toNumber(String.valueOf(number));
	}

	public static String toNumber(String number) {
		number = StringUtils.trim(number);

		if (StringUtils.isNumeric(number)) {
			return number;
		} else {
			return StringUtils.EMPTY;
		}
	}

	public static String toInteger(Object number) {
		return toInteger(String.valueOf(number));
	}

	public static String toInteger(String number) {
		number = StringUtils.trim(number);

		try {
			int integer = Integer.parseInt(number);
			return String.valueOf(integer);
		} catch (NumberFormatException e) {
			return StringUtils.EMPTY;
		}
	}

	public static String toPositiveInteger(Object number) {
		return toPositiveInteger(String.valueOf(number));
	}

	public static String toPositiveInteger(String number) {
		number = StringUtils.trim(number);

		try {
			int integer = Integer.parseInt(number);

			if (integer > -1)
				return String.valueOf(integer);
			else
				return "0";
		} catch (NumberFormatException e) {
			return StringUtils.EMPTY;
		}
	}

}
